<template>
  <div class="home">
    <bread-crumb title="音乐人"></bread-crumb>
    <div class="new_row">
      <el-button type="primary" @click="addShowArea">添加音乐人</el-button>
    </div>
    <el-table :data="datas" border stripe>
      <el-table-column align="center" prop="msc_id" label="音乐人编号" width="50px"></el-table-column>
      <el-table-column align="center" prop="msc_name" label="音乐人" width="250px"></el-table-column>
      <el-table-column align="center" prop="msc_img" label="音乐人图片" width="150px">
        <template slot-scope='scope'>
          <img v-show="scope.row.msc_img" :src="scope.row.msc_img" width="80px" height="80px" style="border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column align="center" prop="msc_hot" label="音乐人热度" width="100px"></el-table-column>
      <el-table-column align="center" prop="msc_fans" label="音乐人粉丝" width="100px"></el-table-column>
      <el-table-column align="center" prop="msc_music" label="音乐" width="<10></10>0px"></el-table-column>
      <el-table-column align="center" prop="msc_tag" label="音乐风格" width="250px"></el-table-column>
      <el-table-column
            fixed="right"
            label="操作"
            width="250">
            <template slot-scope="scope">
              <el-button @click="viewRow(scope.row)" type="primary" size="small">详情</el-button>
              <el-button @click="editRow(scope.row)" type="primary" size="small">编辑</el-button>
              <el-button @click="delRow(scope.row)" type="danger" size="small">删除</el-button>
            </template>
          </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalRows">
     </el-pagination>
     <el-dialog title="删除提醒" :visible.sync="is_show" @close="is_show=false">
       <span class="text-danger">{{ del_msg }}</span>
       <div slot="footer">
         <el-button @click="is_show=false">取消</el-button>
         <el-button @click="delData" type="danger">确定</el-button>
       </div>
     </el-dialog>
     <el-dialog :title="editOrAddTitle" :visible.sync="edit_is_show" @close="editForm = {}">
       <el-form :model="editForm" class="editForm">
         <el-form-item label="音乐人名">
           <el-input v-model="editForm.msc_name"></el-input>
         </el-form-item>
         <el-form-item label="照片">
           <el-input v-model="editForm.msc_img"></el-input>
         </el-form-item>
         <el-form-item label="热度">
           <el-input v-model="editForm.msc_hot"></el-input>
         </el-form-item>
         <el-form-item label="粉丝">
           <el-input v-model="editForm.msc_fans"></el-input>
         </el-form-item>
         <el-form-item label="演出">
           <el-input v-model="editForm.msc_show"></el-input>
         </el-form-item>
         <el-form-item label="详情">
           <el-input type="textmusic" v-model="editForm.msc_details"></el-input>
         </el-form-item>
         <el-form-item label="音乐">
           <el-input v-model="editForm.msc_music"></el-input>
         </el-form-item>
         <el-form-item label="风格">
           <el-input v-model="editForm.msc_tag"></el-input>
         </el-form-item>
       </el-form>
       <div slot="footer">
         <el-button @click="edit_is_show=false">取消</el-button>
         <el-button @click="saveForm" type="primary">确定</el-button>
       </div>
     </el-dialog>
     <el-dialog title="音乐人详情" :visible.sync="view_is_show" @close="view_is_show=false">
       <template>
         <el-tabs :tab-position="tabPosition" style="height: 600px">
           <el-tab-pane label="音乐人信息">
             <span>音乐人id： {{ viewData.add_id }}</span>
             <el-divider></el-divider>
             <span>音乐人名： {{ viewData.msc_name }}</span>
             <el-divider></el-divider>
             <span>音乐人图片：
              <el-image
                 style="width: 100px; height: 100px"
                 :src="viewData.msc_img"
                 :preview-src-list="srcList">
               </el-image>
             </span>
             <el-divider></el-divider>
             <span>音乐人热度： {{ viewData.msc_hot }}</span>
             <el-divider></el-divider>
             <span>粉丝： {{ viewData.msc_fans }}</span>
             <el-divider></el-divider>
             <span>音乐： {{ viewData.msc_music }}</span>
             <el-divider></el-divider>
             <span>风格： {{ viewData.msc_tag }}</span>
             <el-divider></el-divider>
           </el-tab-pane>
           <el-tab-pane label="演出">
             <p>&nbsp;&nbsp; {{ viewData.msc_show }}</p>
           </el-tab-pane>
           <el-tab-pane label="音乐人介绍">
             <p>&nbsp;&nbsp; {{ viewData.msc_details }}</p>
           </el-tab-pane>
         </el-tabs>
       </template>
     </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      datas: [],
      currentPage: 1,
      pageSize: 10,
      totalRows: 0,
      is_show: false,
      view_is_show: false,
      del_msg: '是否删除',
      del_item: {},
      editOrAddTitle: '修改数据',
      edit_is_show: false,
      editForm: {},
      editData: {},
      viewData: {},
      disabled: false,
      tabPosition: 'left',
      srcList: []
    }
  },
  methods: {
    loadData () {
      this.$http.get('/adminapi/music/?page=' + this.currentPage + '&page_size=' + this.pageSize).then(resp => {
        const { results, count } = resp.data
        this.datas = results
        this.totalRows = count
      })
    },
    addShowArea () {
      this.editForm.title = '新增音乐人'
      this.edit_is_show = true
      this.disabled = false
    },
    viewRow (item) {
      this.viewData = item
      this.view_is_show = true
      this.srcList = [item.msc_img]
    },
    editRow (editData) {
      this.edit_is_show = true
      this.disabled = true
      // 初始显示的数据
      this.editForm = {
        msc_name: editData.msc_name,
        msc_id: editData.msc_id,
        msc_img: editData.msc_img,
        msc_hot: editData.msc_hot,
        msc_fans: editData.msc_fans,
        msc_show: editData.msc_show,
        msc_details: editData.msc_details,
        msc_music: editData.msc_music,
        msc_tag: editData.msc_tag,
        show_id: editData.show_id
      }
    },
    saveForm () {
      if (this.disabled) {
        // 删除对象的属性(不修改的)
        const { msc_id } = this.editForm
        delete this.editForm.msc_id
        this.$http.patch('/adminapi/music/' + msc_id + '/', this.editForm).then(resp => {
          console.info(resp.data)
          this.edit_is_show = false
          this.loadData()
          this.editForm = {} // 清空之前绑定的数据
        })
      } else {
        this.$http.post('/adminapi/music/', this.editForm).then(resp => {
          console.info(resp.data)
          this.edit_is_show = false
          this.loadData()
        })
      }
    },
    delRow (item) {
      this.del_item = item
      this.is_show = true
      this.del_msg = '亲，真的忍心删除' + item.msc_name + '吗？'
    },
    delData () {
      this.$http.delete('/adminapi/music/' + this.del_item.msc_id + '/').then(resp => {
        console.info(resp.data)
        this.loadData() // 刷新页面数据，重新装载数据
        this.$message.success('数据删除成功')
        this.is_show = false
      })
    },
    handleSizeChange (size) {
      console.info(size)
      this.currentPage = 1
      this.pageSize = size
      this.loadData()
    },
    handleCurrentChange (page) {
      console.info(page)
      this.currentPage = page
      this.loadData()
    },
    handleClick (tab, event) {
      console.log(tab, event)
    }
  },
  mounted () {
    this.loadData()
  }
}
</script>

<style>
.editForm{
  width: auto;
  height: auto;
}
</style>
